package com.jumi.microservice.controller;

import com.jumi.microservice.common.base.BaseController;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.common.page.TableDataInfo;
import com.jumi.microservice.common.utils.ObjectUtils;
import com.jumi.microservice.common.utils.ResultGenerator;
import com.jumi.microservice.constant.ExceptionEnum;
import com.jumi.microservice.domain.dto.AccountDTO;
import com.jumi.microservice.domain.dto.LogDTO;
import com.jumi.microservice.domain.dto.LoginLogDTO;
import com.jumi.microservice.domain.vo.*;
import com.jumi.microservice.service.AccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * 账号管理controller组件
 *
 * @author Administrator
 */
@Api(tags = "账号管理相关接口")
@RestController
@RequestMapping("/auth/account")
public class AccountController extends BaseController {

    /**
     * 账号管理模块的service组件
     */
    @Autowired
    private AccountService accountService;

    /**
     * 账号分页查询
     *
     * @param accountQuery 账号查询条件
     * @return 角色VO集合
     */
    @ApiOperation(value = "账号分页查询", notes = "获取账号分页数据")
    @GetMapping("/list")
    public TableDataInfo<AccountVO> listAccountByPage(AccountQuery accountQuery) {
        startPage();
        List<AccountDTO> accountDtoList = accountService.listAccountByPage(accountQuery);
        return getDataTable(convertAccountDtoList2VoList(accountDtoList));
    }

    /**
     * 新增账号
     *
     * @param account 账号
     * @return 处理结果, 新增成功返回true
     */
    @ApiOperation(value = "新增账号", notes = "新增账号表记录")
    @PostMapping("/add")
    public ResponseResult<Boolean> saveAccount(@Validated @RequestBody UserAccountVO account, HttpServletRequest request) {
        accountService.saveAccount(convertAccountVo2Dto(account), request);
        return ResultGenerator.genSuccessResult(Boolean.TRUE);
    }

    /**
     * 更新账号
     *
     * @param account 账号
     * @return 处理结果, 新增成功返回true
     */
    @ApiOperation(value = "更新账号", notes = "更新账号表记录")
    @PutMapping("/update")
    public ResponseResult<Boolean> update(@Validated @RequestBody UserAccountVO account, HttpServletRequest request) {
        accountService.updateAccount(convertAccountVo2Dto(account), request);
        return ResultGenerator.genSuccessResult(Boolean.TRUE);
    }

    /**
     * 删除账号
     *
     * @param ids 账号ids,支持批量删除,账号ID以逗号分割
     * @return 处理结果, 删除成功返回true
     */
    @ApiOperation(value = "删除账号数据", notes = "删除指定ID账号表数据,支持批量删除,账号ID以逗号分割")
    @ApiImplicitParam(name = "ids", value = "ids", required = true)
    @DeleteMapping("/delete/{ids}")
    public ResponseResult<Boolean> delete(@PathVariable String ids, HttpServletRequest request) {
        accountService.removeAccountById(ids, request);
        return ResultGenerator.genSuccessResult(Boolean.TRUE);
    }

    /**
     * 根据账号id查询账号详情
     *
     * @param id 账号id
     * @return 账号详情VO
     */
    @ApiOperation(value = "根据账号id查询账号详情", notes = "获取指定id账号详情数据")
    @ApiImplicitParam(name = "id", value = "id", required = true)
    @GetMapping("/detail/{id}")
    public ResponseResult<UserAccountVO> getAccountById(@PathVariable("id") Long id) {
        AccountDTO accountDTO = accountService.getAccountById(id);
        return ResultGenerator.genSuccessResult(convertAccountDto2Vo(accountDTO));
    }

    /**
     * 账号状态启用/停用
     *
     * @param id     账号id
     * @param status 状态 (1启用，0.禁用)
     * @return 处理结果, 更新成功返回true
     */
    @ApiOperation(value = "账号状态启用/停用", notes = "账号状态的启用/停用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true),
            @ApiImplicitParam(name = "status", value = "状态", required = true)
    })
    @PutMapping("/enabled")
    public ResponseResult<Boolean> enableStatus(@RequestParam("id") Long id, @RequestParam("status") Integer status, HttpServletRequest request) {
        accountService.enableStatus(id, status, request);
        return ResultGenerator.genSuccessResult(Boolean.TRUE);
    }

    /**
     * 账号登录次数分页查询
     *
     * @param loginLogQuery 账号查询条件
     * @return 账号登录次数日志集合
     */
    @ApiOperation(value = "账号登录次数分页查询", notes = "获取账号登录次数分页数据")
    @GetMapping("/login")
    public TableDataInfo<LoginLogVO> listAccountLoginByPage(LoginLogQuery loginLogQuery) {
        startPage();
        List<LoginLogDTO> loginLogDtoList = accountService.listAccountLoginByPage(loginLogQuery);
        return getDataTable(convertAccountLogDtoList2VoList(loginLogDtoList));
    }

    /**
     * 账号操作日志分页查询
     *
     * @param operateLogQuery 账号查询条件
     * @return 账号操作日志集合
     */
    @ApiOperation(value = "账号操作日志分页查询", notes = "获取账号操作日志分页数据")
    @GetMapping("/log")
    public TableDataInfo<LoginLogVO> listAccountOperateLogByPage(OperateLogQuery operateLogQuery) {
        startPage();
        List<LogDTO> logDtoList = accountService.listAccountOperateLogByPage(operateLogQuery);
        return getDataTable(convertOperateLogDtoList2VoList(logDtoList));
    }

    /**
     * 将账号VO对象转换为DTO对象
     *
     * @param account 账号VO
     * @return AccountDTO 账号DTO
     */
    private AccountDTO convertAccountVo2Dto(UserAccountVO account) {
        AccountDTO targetAccount = null;
        try {
            targetAccount = account.clone(AccountDTO.class);
        } catch (Exception e) {
            ExceptionEnum.FAIL_CHANGE.newException();
        }
        return targetAccount;
    }

    /**
     * 将账号DTO对象转换为VO对象
     *
     * @param account 账号DTO
     * @return AccountVO 账号vo
     */
    private UserAccountVO convertAccountDto2Vo(AccountDTO account) {
        UserAccountVO targetAccount = null;
        try {
            targetAccount = account.clone(UserAccountVO.class);
        } catch (Exception e) {
            ExceptionEnum.FAIL_CHANGE.newException();
        }
        return targetAccount;
    }

    /**
     * 将账号DTO对象集合转换为VO对象集合
     *
     * @param accounts 账号DTO集合
     * @return List<AccountVO> 账号VO对象集合
     */
    private List<AccountVO> convertAccountDtoList2VoList(List<AccountDTO> accounts) {
        List<AccountVO> accountVoList = null;
        try {
            accountVoList = ObjectUtils.convertList(accounts, AccountVO.class);
        } catch (Exception e) {
            ExceptionEnum.FAIL_CHANGE.newException();
        }
        return accountVoList;
    }

    /**
     * 将账号登录日志DTO对象集合转换为VO对象集合
     *
     * @param logs 账号登录日志DTO集合
     * @return List<LoginLogVO> 账号DTO对象集合
     */
    private List<LoginLogVO> convertAccountLogDtoList2VoList(List<LoginLogDTO> logs) {
        List<LoginLogVO> logVoList = null;
        try {
            logVoList = ObjectUtils.convertList(logs, LoginLogVO.class);
        } catch (Exception e) {
            ExceptionEnum.FAIL_CHANGE.newException();
        }
        return logVoList;
    }

    /**
     * 将账号操作日志DTO对象集合转换为VO对象集合
     *
     * @param logs 账号操作日志DTO集合
     * @return List<LogVO> 账号DTO对象集合
     */
    private List<LogVO> convertOperateLogDtoList2VoList(List<LogDTO> logs) {
        List<LogVO> logVoList = null;
        try {
            logVoList = ObjectUtils.convertList(logs, LogVO.class);
        } catch (Exception e) {
            ExceptionEnum.FAIL_CHANGE.newException();
        }
        return logVoList;
    }
}
